// page/diary-write/index.js
const config = require('../../config')
const common = require('../../util/common');
const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnCan: false,
    loadingHidden: true,
    showLeft: false,
    showRight: false,
    today_date: getDate(),
    today_week: getWeek(),
    length: 0,
    content:"",
    isPassCheck: true,
    post_data:{
      userid: "",
      weather: "-1",
      mood: "-1",
      content: ""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    // this.data.today_date=new Date();
    
  },

  // 统计字数
  countNum: function (e) {
    // console.log(e.detail.value.length);
    var num = e.detail.value.length;
    this.setData({
      length: num
    });
    this.data.post_data.content = e.detail.value;
    // console.log(this.data.post_data.content);
  },
  
  save:async function() {
    var that = this;
    var content = that.data.post_data.content;
    var weather = that.data.post_data.weather;
    var mood = that.data.post_data.mood;
    that.setData({
      btnCan: true,
      loadingHidden: false
    })
    // 敏感内容检测
    wx.cloud.callFunction({
      name: 'contentCheck',
      data: {
        msg: content
      },
      success: function(res) {
        db.collection('diary').add({
          data: {
            openid: app.globalData.openid,
            createDateStr: that.data.today_date,
            weather: weather,      
            mood: mood,
            week: that.data.today_week,
            content: content,
            createDate: new Date(new Date().toLocaleDateString()),
            createTime: db.serverDate()
          },
          success: function(res){
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 1000
            });
            wx.setStorage({
              key: 'refresh',
              data: true,
            })
            wx.setStorage({
              key: 'refreshDiaryCount',
              data: true
            })
            setTimeout(function(){
              // 返回到上一页
              wx.navigateBack({
                delta: 1,
              })
            },700);             
          }      
        })
      },
      fail: function(err) {
        wx.showToast({
          title: '含敏感内容',
          icon: 'error',
          duration: 1500
        });
      },
      complete: function() {
        that.setData({
          btnCan: false,
          loadingHidden: true
        })
      }
    })
    // var isPassCheck = await common.contentCheck(content)
    // if (isPassCheck) {
    //   db.collection('diary').add({
    //     data: {
    //       openid: app.globalData.openid,
    //       createDateStr: that.data.today_date,
    //       weather: weather,      
    //       mood: mood,
    //       week: that.data.today_week,
    //       content: content,
    //       createDate: new Date(new Date().toLocaleDateString()),
    //       createTime: db.serverDate()
    //     },
    //     success: function(res){
    //       wx.showToast({
    //         title: '保存成功',
    //         icon: 'success',
    //         duration: 1000
    //       });
    //       wx.setStorage({
    //         key: 'refresh',
    //         data: true,
    //       })
    //       wx.setStorage({
    //         key: 'refreshDiaryCount',
    //         data: true
    //       })
    //       setTimeout(function(){
    //         // 返回到上一页
    //         wx.navigateBack({
    //           delta: 1,
    //         })
    //       },700);             
    //     }      
    //   })
    // } else {
    //   wx.showToast({
    //     title: '含敏感内容',
    //     icon: 'error',
    //     duration: 1500
    //   });
    // }
  },
  
})

// 获得年月日
function getDate() {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1;
  var day = myDate.getDate();
  return year + "年" + month + "月" + day + "日";
}

// 获得星期
function getWeek() {
  var weeks = ["天","一","二","三","四","五","六"];
  return "星期"+weeks[new Date().getDay()];
}